<template>
    <div>
        <h1 style="text-align: center;">用户新增</h1>
        <el-form ref="form" :model="form" label-width="80px">
            <el-form-item label="用户名">
                <el-input v-model="form.username"></el-input>
            </el-form-item>
            <el-form-item label="密码">
                <el-input type="password" v-model="form.password"></el-input>
            </el-form-item>
            <el-form-item label="邮箱">
                <el-input v-model="form.email"></el-input>
            </el-form-item>
            <el-form-item label="手机号">
                <el-input v-model="form.tel"></el-input>
            </el-form-item>
            <el-form-item label="是否启用">
                <el-switch
                    v-model="form.enable"
                    active-color="#13ce66"
                    inactive-color="#ff4949">
                </el-switch>
            </el-form-item>
            <el-form-item label="角色">
                <el-select v-model="form.role" placeholder="请选择角色">
                <el-option v-for="role in rolelist"  v-show="role.name != '超级' " :key="role._id" :label="role.name" :value="role._id"></el-option>
                </el-select>
            </el-form-item>
            <el-form-item>
                <el-button type="primary" @click="onSubmit">立即创建</el-button>
                <el-button @click="$router.back()">取消</el-button>
            </el-form-item>
        </el-form>
    </div>
</template>

<script>
import { roleList, userAdd } from '@/request'
export default {
    data () {
        return {
            form: {
                username: '',
                password: '',
                email: '',
                tel: '',
                enable: false,
                role: ''
            },
            rolelist:[]
        }
    },
    methods: {
        async onSubmit() {
            // console.log(this.form);
            let data =  await userAdd(this.form)
            // console.log(data);
            let { error_code,msg } = data
            if(error_code === 200 ){
                this.$message({
                    type: 'success',
                    duration: 1500,
                    message: msg,
                    onClose: () => {
                        this.$router.push('/user/list')
                    }
                })
            } else {
                this.$message({
                    type: 'error',
                    duration: 1500,
                    message: msg,
                })
            }
        },
      async GetRoleList() {
        let data = await roleList()
        this.rolelist = data.data.rolelist
        // console.log(this.userlist);
      }
    },
    created() {
        this.GetRoleList()
    }
}
</script>

<style>
    
</style>
